package com.leyou.search.web;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * @ClassName : SearchController  //类名
 * @Description :   //描述
 * @Author : qingfeng  //作者
 * @Date: 2020-09-19 19:45  //时间
 */
@RestController
@RequestMapping("goods")
public class SearchController {
    private final SearchService searchService;

    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    /**
     * 初始化索引库
     */
    @GetMapping("/initialization")
    public ResponseEntity<String> init() {
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }

    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key")String key){
        /**
         * @Description //TODO 查询自动补全数据
         * @Param [key]
         * @return reactor.core.publisher.Mono<java.util.List < java.lang.String>>
         */
        return searchService.getSuggestion(key);
    }

    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO request) {
        /**
         * @Description //TODO 分页搜索商品数据
         * @Param [request]
         * @return reactor.core.publisher.Mono<com.leyou.starter.elastic.entity.PageInfo < com.leyou.search.entity.Goods>>
         */
        return searchService.searchGoods(request);
    }

    @PostMapping("/filter")
    public Mono<Map<String,List<?>>> getFilters(@RequestBody SearchParamDTO param){
        /**
         * @Description //TODO 查询过滤项
         * @Param [param]
         * @return reactor.core.publisher.Mono<java.util.Map < java.lang.String, java.util.List < ?>>>
         */
        return searchService.getFilters(param);
    }
}
